# encoding: UTF-8
import sys
from PyQt4.QtGui import *
from PyQt4.QtSql import *

class PortfolioManage(QWidget):
    def __init__(self,path,parent=None):

        QWidget.__init__(self, parent)
        self.resize(700,200)
        self.setWindowTitle(u"管理交易组合")
        self.path = path
        self.db = None


        self.view = QTableView()
        self.initDb()
        layout = QVBoxLayout(self)
        layout.addWidget(self.view)

        btnLayout = QHBoxLayout(self)
        btnLayout.addStretch(1)
        addBtn = QPushButton(u"添加组合")
        addBtn.clicked.connect(self.onAdd)
        btnLayout.addWidget(addBtn)

        closeBtn = QPushButton(u"关闭")
        closeBtn.clicked.connect(self.onClose)
        btnLayout.addWidget(closeBtn)

        layout.addLayout(btnLayout)
        self.setLayout(layout)


    def createTable(self):
        query = QSqlQuery(self.db)
        query.exec_(
            '''
            CREATE TABLE "portfolios" (
                "id" INTEGER PRIMARY KEY AUTOINCREMENT,
                portfolio_name varchar(100) NOT NULL,
                portfolio_desc varchar(200) NOT NULL,
                init_cash decimal(15,5) DEFAULT '0.00000',
                cur_cash decimal(15,5) DEFAULT '0.00000',
                equity decimal(15,5) DEFAULT '0.00000',
                unrealised_pnl decimal(15,5) DEFAULT '0.00000',
                realised_pnl decimal(15,5) DEFAULT '0.00000',
                trade_account_id int(11) NOT NULL,
                positions text NOT NULL
            );

           '''
        )
        query.exec_(
            '''
            INSERT INTO "portfolios" VALUES (22,'方法','343434让人',4343434.00000,0.00000,0.00000,0.00000,0.00000,33,''),(23,'组合名','11111',333333.00000,0.00000,0.00000,0.00000,0.00000,32,''),
            (24,'新兴行业','新兴行业寻找业绩预期长远，长期预期的标的，滚动操作',500000.00000,1000.00000,0.00000,0.00000,0.00000,34,'[]'),(26,'困境反转','困境反转策略',1000000.00000,0.00000,0.00000,0.00000,0.00000,34,'');
            '''
        )



    def initDb(self):

        if self.db:
            self.closeDatabase()

        self.db = QSqlDatabase.addDatabase("QSQLITE", "db1")
        self.db.setDatabaseName(self.path)
        if not self.db.open("", ""):
            QMessageBox.warning(self, "PyQT", u"链接数据库失败", QMessageBox.Yes, QMessageBox.No)
            return None
        self.createTable()
        self.model = QSqlTableModel(None, self.db)
        self.model.setTable("portfolios")
        self.model.select()

        self.view.setModel(self.model)


    def onAdd(self):
        rowPosition = self.model.rowCount()
        self.model.insertRow(rowPosition)




    def closeDatabase(self):

        self.view.setModel(None)
        del self.model
        self.db.close()
        del self.db
        QSqlDatabase.removeDatabase("db1")

    def onClose(self):

        self.closeDatabase()
        self.close()


if __name__ == "__main__":

    app = QApplication(sys.argv)


    window = PortfolioManage("C:/Users/jacob/test2.db")
    window.show()
    sys.exit(app.exec_())